package com.baidu.ueditor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.define.ActionMap;

import java.util.HashMap;
import java.util.Map;


/**
 * 配置管理器
 *
 * @author hancong03@baidu.com
 */
public final class ConfigManager {
    private static ConfigManager INSTANCE;

    private JSONObject jsonConfig = UEditorConfig.getCONFIG();
    // 涂鸦上传filename定义
    private final static String SCRAWL_FILE_NAME = "scrawl";
    // 远程图片抓取filename定义
    private final static String REMOTE_FILE_NAME = "remote";

    private ConfigManager() {
        init();
    }


    private Map<String, Object> upload_file_config = null;
    private Map<String, Object> upload_image_config = null;
    private Map<String, Object> upload_media_config = null;
    private Map<String, Object> upload_scrawl_config = null;
    private Map<String, Object> catch_image_config = null;
    private Map<String, Object> list_image_config = null;
    private Map<String, Object> list_file_config = null;

    /**
     * 配置管理器构造工厂
     *
     * @return 配置管理器实例或者null
     */
    public static ConfigManager getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ConfigManager();
        }
        return INSTANCE;
    }

    public void init() {
        upload_file_config = new HashMap<>();
        upload_file_config.put("isBase64", "false");
        upload_file_config.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
        upload_file_config.put("allowFiles", this.getArray("fileAllowFiles"));
        upload_file_config.put("fieldName", this.jsonConfig.getString("fileFieldName"));
        upload_file_config.put("savePath", this.jsonConfig.getString("filePathFormat"));
        upload_file_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        upload_image_config = new HashMap<>();
        upload_image_config.put("isBase64", "false");
        upload_image_config.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
        upload_image_config.put("allowFiles", this.getArray("imageAllowFiles"));
        upload_image_config.put("fieldName", this.jsonConfig.getString("imageFieldName"));
        upload_image_config.put("savePath", this.jsonConfig.getString("imagePathFormat"));
        upload_image_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        upload_media_config = new HashMap<>();
        upload_media_config.put("maxSize", this.jsonConfig.getLong("mediaMaxSize"));
        upload_media_config.put("allowFiles", this.getArray("mediaAllowFiles"));
        upload_media_config.put("fieldName", this.jsonConfig.getString("mediaFieldName"));
        upload_media_config.put("savePath", this.jsonConfig.getString("mediaPathFormat"));
        upload_media_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        upload_scrawl_config = new HashMap<>();
        upload_scrawl_config.put("filename", ConfigManager.SCRAWL_FILE_NAME);
        upload_scrawl_config.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
        upload_scrawl_config.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
        upload_scrawl_config.put("isBase64", "true");
        upload_scrawl_config.put("savePath", this.jsonConfig.getString("scrawlPathFormat"));
        upload_scrawl_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        catch_image_config = new HashMap<>();
        catch_image_config.put("filename", ConfigManager.REMOTE_FILE_NAME);
        catch_image_config.put("filter", this.getArray("catcherLocalDomain"));
        catch_image_config.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
        catch_image_config.put("allowFiles", this.getArray("catcherAllowFiles"));
        catch_image_config.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");
        catch_image_config.put("savePath", this.jsonConfig.getString("catcherPathFormat"));
        catch_image_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        list_image_config = new HashMap<>();
        list_image_config.put("allowFiles", this.getArray("imageManagerAllowFiles"));
        list_image_config.put("dir", this.jsonConfig.getString("imageManagerListPath"));
        list_image_config.put("count", this.jsonConfig.getIntValue("imageManagerListSize"));
        list_image_config.put("savePath", this.jsonConfig.getString("catcherPathFormat"));
        list_image_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());

        list_file_config = new HashMap<>();
        list_file_config.put("allowFiles", this.getArray("fileManagerAllowFiles"));
        list_file_config.put("dir", this.jsonConfig.getString("fileManagerListPath"));
        list_file_config.put("count", this.jsonConfig.getIntValue("fileManagerListSize"));
        list_file_config.put("rootPath", UEditorConfig.getGLOBAL_SAVE_PATH_PREFFIX());
    }

    // 验证配置文件加载是否正确
    public boolean valid() {
        return this.jsonConfig != null;
    }

    public JSONObject getAllConfig() {
        return this.jsonConfig;
    }

    public Map<String, Object> getConfig(int type) {
        switch (type) {
            case ActionMap.UPLOAD_FILE:
                return upload_file_config;
            case ActionMap.UPLOAD_IMAGE:
                return upload_image_config;

            case ActionMap.UPLOAD_MEDIA:
                return upload_media_config;

            case ActionMap.UPLOAD_SCRAWL:
                return upload_scrawl_config;

            case ActionMap.CATCH_IMAGE:
                return catch_image_config;

            case ActionMap.LIST_IMAGE:
                return list_image_config;

            case ActionMap.LIST_FILE:
                return list_file_config;
            default:
                return null;
        }

    }


    private String[] getArray(String key) {
        JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
        String[] result = new String[jsonArray.size()];

        for (int i = 0, len = jsonArray.size(); i < len; i++) {
            result[i] = jsonArray.getString(i);
        }
        return result;
    }

}
